package com.space.service.impl;

import com.space.dao.UserDao;
import com.space.entity.User;
import com.space.exception.MyException;
import com.space.message.MyMessage;
import com.space.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 港某
 * @date : 2020/2/13 9:43
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public User selectUserByAccount(String userAccount) {
        return userDao.selectUserByAccount(userAccount);
    }

    @Override
    public List<User> selectAllUser() {
        return userDao.selectAllUser();
    }

    @Override
    public void insertUser(User user) {
        userDao.insertUser(user);
    }

    @Override
    public void updateUserPwd(User user) {
        User newUser = userDao.selectUserByAccount(user.getUserAccount());
        if (newUser != null && newUser.getUserStatus().getStatusTypeId() != 2) {
            userDao.updateUserPwd(user);
        }
    }

    @Override
    public void updateUserPhone(User user) {
        userDao.updateUserPhone(user);
    }

    @Override
    public String checkUserInfo(String userAccount, String userPwd) {
        User user = userDao.selectUserByAccount(userAccount);
        if (user == null) {
            return MyMessage.USER_ACCOUNT_ERROR;
        }
        if (!user.getUserPwd().equals(userPwd)) {
            return MyMessage.USER_PASSWORD_ERROR;
        }
        if (user.getUserStatus().getStatusTypeId() != 1) {
            return MyMessage.USER_STATUS_ERROR;
        }
        return "";
    }

    @Override
    public String checkChangePwd(String userAccount, String userPhone) {
        User user = userDao.selectUserByAccount(userAccount);
        if (user == null) {
            return MyMessage.USER_ACCOUNT_ERROR;
        }
        if (!user.getUserPhone().equals(userPhone)) {
            return MyMessage.USER_PHONE_ERROR;
        }
        return null;
    }

    @Override
    public String checkUserPhone(String userId, String userPhone) {
        User user = userDao.selectUserByPhone(userPhone);
        if (user != null) {
            return MyMessage.PHONE_REPEAT_ERROR;
        }
        return null;
    }

    @Override
    public User userLogin(User user) {
        if (user.getUserAccount() != null) {
            User newUser = userDao.selectUserByAccount(user.getUserAccount());
            if (newUser != null) {
                if (newUser.getUserPwd().equals(user.getUserPwd())) {
                    return newUser;
                }
            }
        }
        return null;
    }

    @Override
    public String checkUserRegister(String userAccount, String userPhone) {
        User user1 = userDao.selectUserByAccount(userAccount);
        if (user1 != null) {
            return MyMessage.USER_ACCOUNT_REPEAT_ERROR;
        }
        User user2 = userDao.selectUserByPhone(userPhone);
        if (user2 != null) {
            return MyMessage.PHONE_REPEAT_ERROR;
        }
        return "";
    }

    @Override
    public int selectAllUserCount() {
        return userDao.selectAllUserCount();
    }

    @Override
    public void updateUserStatusById(int[] userId, int userStatusId) {
        for (int value : userId) {
            Map<String, Integer> map = new HashMap<>();
            map.put("userId", value);
            map.put("userStatusId", userStatusId);
            userDao.updateUserStatusById(map);
        }
    }

    @Override
    public User selectUserById(int userId) {
        return userDao.selectUserById(userId);
    }

    @Override
    public void updateUserInfo(User oldUser, User user, String userPwd2) throws MyException {
        if (oldUser == null || user == null) {
            throw new MyException(MyMessage.UNKNOWN_ERROR);
        }
        if (oldUser.getUserId() != user.getUserId()) {
            throw new MyException(MyMessage.UNKNOWN_ERROR);
        }
        if (!oldUser.getUserAccount().equals(user.getUserAccount())) {
            throw new MyException(MyMessage.UNKNOWN_ERROR);
        }
        if (!user.getUserPwd().equals(userPwd2)) {
            throw new MyException(MyMessage.PASSWORD_REPEAT_ERROR);
        }
        if (!oldUser.getUserPhone().equals(user.getUserPhone())) {
            User tmpUser = userDao.selectUserByPhone(user.getUserPhone());
            if (tmpUser != null) {
                throw new MyException(MyMessage.PHONE_REPEAT_ERROR);
            }
        }
        userDao.updateUser(user);
    }
}
